﻿using IoTGateway.Model;
using OpenIotMain.SQLiteTool;
using OpenIotModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpenIotMain {
    public partial class DeviceManageForm : Form {
        SqliteDbContext dbcontext = new SqliteDbContext();
        public DeviceManageForm() {
            InitializeComponent();
            DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
            checkBoxColumn.HeaderText = "选择"; // 列的标题
            checkBoxColumn.Name = "checkBoxColumn"; // 列的名称，可以用于引用这个列

            // 将列添加到 dataGridView1 中
            dataGridView1.Columns.Add(checkBoxColumn);

            // 设置列的位置（可选）
            checkBoxColumn.DisplayIndex = 0; // 这里假设你希望选择框列显示在第一列

            // 允许用户选择多行（可选）
            dataGridView1.MultiSelect = true;

        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e) {
            try {
                Device device = new Device {
                    ID = Guid.NewGuid().ToString(),
                    DeviceName = textBox1.Text,
                    Description = textBox2.Text,
                    Index = 0,
                    AutoStart = false,
                    CmdPeriod = 0,
                    DriverName = textBox3.Text,
                    version = textBox4.Text,
                    ip = textBox5.Text,
                    port = Convert.ToInt32(textBox6.Text),
                    slot = Convert.ToByte(textBox7.Text),
                    rack = Convert.ToByte(textBox8.Text)
                };
                dbcontext.device.Add(device);
                dbcontext.SaveChanges();
            }
            catch {
            }

            MessageBox.Show("添加成功");
            var list = dbcontext.device.ToList();
            dataGridView1.DataSource = list;
        }

        private void DeviceManageForm_Load(object sender, EventArgs e) {
            var list = dbcontext.device.ToList();
            dataGridView1.DataSource = list;
            dataGridView1.Columns["ID"].Visible = false;
            dataGridView1.Columns["Index"].Visible = false;
            dataGridView1.Columns["AutoStart"].Visible = false;
            dataGridView1.Columns["CmdPeriod"].Visible = false;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e) {
            foreach (DataGridViewRow row in dataGridView1.Rows) {
                DataGridViewCheckBoxCell checkBoxCell = (DataGridViewCheckBoxCell)row.Cells[0];
                if (checkBoxCell.Value != null && (bool)checkBoxCell.Value) {
                    //selectedRows.Add(row);
                    string id = row.Cells[1].Value.ToString();
                    Device dvice = dbcontext.device.Find(id);
                    dbcontext.device.Remove(dvice);
                }
            }
            dbcontext.SaveChanges();
            MessageBox.Show("删除成功");
            var list = dbcontext.device.ToList();
            dataGridView1.DataSource = list;
        }
    }
}
